//
//  CZUserPasswordLoginViewController.m
//  nsj_iOS
//
//  Created by apple on 2016/12/27.
//  Copyright © 2016年 GCZ. All rights reserved.
//

#import "CZUserPasswordLoginViewController.h"
#import "CZPasswordLoginView.h"
#import "CZUserForgetPasswordViewController.h"
#import "CZUserRegisterAccountViewController.h"
#import "CZBaseTabBarController.h"
#import "CZMessionCenterViewController.h"
#import "CZMineViewController.h"
@interface CZUserPasswordLoginViewController ()
@property (nonatomic, strong) CZPasswordLoginView *pwdLoginView;

@end

@implementation CZUserPasswordLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getTopImage];
    self.title = @"密码登录";
    CZPasswordLoginView *pwdLoginView = [[[NSBundle mainBundle] loadNibNamed:@"CZPasswordLoginView" owner:nil options:nil] firstObject];
    _pwdLoginView = pwdLoginView;
    [pwdLoginView.loginBtn addTarget:self action:@selector(login) forControlEvents:(UIControlEventTouchUpInside)];
    [pwdLoginView.forgetPassword addTarget:self action:@selector(forgetPassword) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:pwdLoginView];
    [pwdLoginView.registerBtn addTarget:self action:@selector(registerAccount) forControlEvents:(UIControlEventTouchUpInside)];
    [pwdLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    // Do any additional setup after loading the view.
}

#pragma mark- 登录 忘记密码 注册账号
- (void)login {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/user/login",APP_HOST];
    if (_pwdLoginView.phoneNumberTextField.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号"];
        return;
    }
    if (_pwdLoginView.passWordTextField.text.length < 6 || _pwdLoginView.passWordTextField.text.length > 12) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的密码"];
        return;
    }
    
    NSDictionary *params;
    if ([[NSUserDefaults standardUserDefaults] objectForKey:mRegisterId ]) {
        params = @{@"login_id" : _pwdLoginView.phoneNumberTextField.text, @"password" : _pwdLoginView.passWordTextField.text, @"login_type" : @2, @"registerid" : [[NSUserDefaults standardUserDefaults] objectForKey:mRegisterId ]};

    } else {
        params = @{@"login_id" : _pwdLoginView.phoneNumberTextField.text, @"password" : _pwdLoginView.passWordTextField.text, @"login_type" : @2};
    }
    [SVProgressHUD showWithStatus:@"正在登录..."];
    [GCZAFNetWorking POST:urlString params:params success:^(NSDictionary *response) {
        if ([response[@"code"] isEqualToString:@"200"]) {
            [CZNsjHelper setUserInfo:self.navigationController dataDic:response];
            CZBaseTabBarController *rootVC = (CZBaseTabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
            CZNavigationController *naviVC = rootVC.childViewControllers[0];
            CZMessionCenterViewController *homeVC = naviVC.childViewControllers[0];
            
            CZNavigationController *mineNC = rootVC.childViewControllers[4];
            CZMineViewController *mineVC = mineNC.childViewControllers[0];
            [mineVC getUserData];
            mineVC.userIconImageView.yy_imageURL = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:User_Logo]];
            [homeVC setUserIconImage];
            if (self.loginCallback) {
                self.loginCallback();
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:CZLoginSuccessNotification object:nil];

            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"message"]];
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
        DLog(@"%@", error);
    }];
}

#pragma mark- 忘记密码 注册账号
- (void)forgetPassword {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)registerAccount {
    CZUserRegisterAccountViewController *registerAccountVC = [[CZUserRegisterAccountViewController alloc] init];
    [self.navigationController pushViewController:registerAccountVC animated:YES];
}

#pragma mark - 获取头部的图片
- (void)getTopImage {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/doujin/random-doujin",APP_HOST];
    [GCZAFNetWorking GET:urlString success:^(NSDictionary *response) {
        if ([response[@"code"] isEqualToString:@"200"]) {
            NSString *authorStr = [NSString stringWithFormat:@"作者:%@", response[@"result"][@"author"]];
            if ([response[@"result"][@"share_pics"] containsString:@"http"]) {
                _pwdLoginView.authorLabel.text = authorStr;
                _pwdLoginView.topImageView.yy_imageURL = [NSURL URLWithString:[response[@"result"][@"share_pics"] stringByAppendingString:@"?x-oss-process=style/ios_doujin_50"]];
            }
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
    
}
- (void)dealloc {
    DLog(@"%s", __func__);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
